#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
   echo "This app dosen't support macOS."
   exit 1
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

APP_INSTALL_NAME="minimax-remover"
APP_TARGET_DIR_NAME="minimax-remover"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo $CURRENT_APP_PATH
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist, please retry. "$CURRENT_APP_PATH
    exit 1
fi

echo "lmdprogress:12"

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL="${GIT_HOST}/zibojia/MiniMax-Remover"


export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:20"

conda --version
echo "lmdprogress:25"
cd "$CURRENT_APP_PATH"

export CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi

export PYTHON_VER_ENV=3.12
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV -y

echo "lmdprogress:30"

cd "$CURRENT_APP_PATH"


# source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_win_fixed_ver.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_ffmpeg.sh"

echo "lmdprogress:40"

cd "$CURRENT_APP_PATH"
install_requirements() {
  conda_run_pip_install --upgrade packaging modelscope huggingface_hub hf_xet gradio_client gradio==5.34.2 pillow==10.4
  grep -vE 'torch==|torchvision==|huggingface_hub==|gradio==|gradio_client==|Pillow==' requirements.txt > requirements-temp.txt
  conda_run_pip_install -r requirements-temp.txt
}

install_requirements

echo "lmdprogress:50"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

echo "HF_ENDPOINT "$HF_ENDPOINT

install_models() {
  echo "start download models"
  
  cd "$CURRENT_APP_PATH/gradio_demo"
  export CONDA_PREFIX="../venv"
  export HF_HOME="./tmp_hf_home"
  export TORCH_HOME="./tmp_torch_home"
  export GRADIO_TEMP_DIR="./gradio_temp"
  # export GRADIO_TEMP_DIR="$CURRENT_APP_PATH/gradio_temp"
  
  conda_run_hf_download facebook/sam2-hiera-large --local-dir ./SAM2-Video-Predictor/checkpoints/

  echo "lmdprogress:80"
  conda_run_hf_download zibojia/minimax-remover --local-dir ./model/

}

install_models


echo "lmdprogress:100"
